<template>
  <ve-line :colors="colors" :data="chartData" :extend="extendsConfig" :loading="loading" :settings="chartSettings" />
</template>

<script>
import VeLine from 'v-charts/lib/line.common';
import echarts from 'echarts';
import 'v-charts/lib/style.css';

export default {
  components: {
    VeLine
  },
  props: {
    linkUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: true,
      chartSettings: {
        labelMap: {
          date: '日期',
          pv: '访问量(PV)',
          uv: '独立访客数(UV)'
        },
        xAxisType: 'category',
        yAxisType: ['KMB'],
        area: true,
        scale: [true, false]
      },
      chartData: {
        columns: ['date', 'pv', 'uv'],
        rows: []
      },
      colors: [
        // 颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
        new echarts.graphic.LinearGradient(0, 1, 0, 0, [
          {
            offset: 0,
            color: '#C8E3FF'
          },
          {
            offset: 0.6,
            color: '#C8E7FF'
          }
        ]),
        new echarts.graphic.LinearGradient(0, 1, 0, 0, [
          {
            offset: 0,
            color: '#FFEAD6'
          },
          {
            offset: 0.6,
            color: '#FFDBA8'
          }
        ])
      ],
      extendsConfig: {
        grid: {
          right: 20
        },
        xAxis(v) {
          v.forEach(i => {
            i.boundaryGap = false;
            i.axisLabel = {
              showMaxLabel: true,
              rotate: 45
            };
          });

          return v;
        },
        yAxis(v) {
          v.forEach(i => {
            i.minInterval = 1;
          });

          return v;
        }
      }
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    changeOrderTime: function (time) {
      this.loadData();
    },
    loadData: function () {
      this.$http.get(
          '/admin/data/website/visit',
          {
            time: 'today'
          },
          response => {
            this.chartData.rows = response.data.rows;
            this.loading = false;
          },
          _ => {
            this.loading = false;
          },
          false
      );
    }
  }
};
</script>

<style></style>
